home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Cream of the Crop 1
/
Cream of the Crop 1.iso
/
WINDOWS
/
MSJV7_1.ARJ
/
EDITFILE.C
< prev
next >
Wrap
Text File
|
1992-01-01
|
16KB
|
492 lines
case IDM_OPEN:
if (!QuerySaveFile(hWnd))
return (NULL);
lpOpenDlg = MakeProcInstance((FARPROC) OpenDlg, hInst);
/* Open the file and get its handle */
hFile = DialogBox(hInst, "Open", hWnd, lpOpenDlg);
FreeProcInstance(lpOpenDlg);
if (!hFile)
return (NULL);
/* Allocate edit buffer to the size of the file + 1 */
hEditBuffer =
LocalAlloc(LMEM_MOVEABLE | LMEM_ZEROINIT,
(WORD)(FileStatus.st_size+1));
if (!hEditBuffer) {
MessageBox(hWnd, "Not enough memory.",
NULL, MB_OK | MB_ICONHAND);
return (NULL);
}
hSaveCursor = SetCursor(hHourGlass);
pEditBuffer = LocalLock(hEditBuffer);
IOStatus = read(hFile, pEditBuffer, FileStatus.st_size);
close(hFile);
/* # bytes read must equal file size */
if (IOStatus != FileStatus.st_size) {
sprintf(str, "Error reading %s.", FileName);
SetCursor(hSaveCursor); /* Remove the hourglass */
MessageBox(hWnd, str, NULL, MB_OK | MB_ICONEXCLAMATION);
}
LocalUnlock(hEditBuffer);
/* Set up a new buffer and window title */
sprintf(str, "EditFile - %s", FileName);
SetNewBuffer(hWnd, hEditBuffer, str);
SetCursor(hSaveCursor); /* restore the cursor */
break;
/****************************************************************************
FUNCTION: TraceOutput(HWND, unsigned, WORD, LONG)
PURPOSE: Let user select a file, and open it.
****************************************************************************/
HANDLE FAR PASCAL TraceOutput(hDlg, message, wParam, lParam)
HWND hDlg;
unsigned message;
WORD wParam;
LONG lParam;
{
WORD index;
PSTR pTptr;
HANDLE hFile;
switch (message) {
case WM_COMMAND:
switch (wParam) {
case IDC_LISTBOX:
switch (HIWORD(lParam)) {
case LBN_SELCHANGE:
/* If item is a directory name, append "*.*" */
if (DlgDirSelect(hDlg, str, IDC_LISTBOX))
strcat(str, DefSpec);
SetDlgItemText(hDlg, IDC_EDIT, str);
SendDlgItemMessage(hDlg,
IDC_EDIT,
EM_SETSEL,
NULL,
MAKELONG(0, 0x7fff));
break;
case LBN_DBLCLK:
goto openfile;
}
return (TRUE);
case IDD_OK:
openfile:
GetDlgItemText(hDlg, IDC_EDIT, OpenName, 128);
if (strchr(OpenName, '*') || strchr(OpenName, '?')) {
SeparateFile(hDlg, (LPSTR) str, (LPSTR) DefSpec,
(LPSTR) OpenName);
if (str[0])
strcpy(DefPath, str);
ChangeDefExt(DefExt, DefSpec);
UpdateListBox(hDlg);
return (TRUE);
}
if (!OpenName[0]) {
MessageBox(hDlg, "No filename specified.",
NULL, MB_OK | MB_ICONHAND);
return (TRUE);
}
AddExt(OpenName, DefExt);
/* Open the file */
if ((hFile = OpenFile(OpenName, (LPOFSTRUCT) &OfStruct,
OF_READ)) == -1) {
sprintf(str, "Error %d opening %s.",
OfStruct.nErrCode, OpenName);
MessageBox(hDlg, str, NULL,
MB_OK | MB_ICONHAND);
}
else {
/* Make sure there's enough room for the file */
fstat(hFile, &FileStatus);
if (FileStatus.st_size > MAXFILESIZE) {
sprintf(str,
"Not enough memory to load %s.\n%s exceeds %ld bytes.",
OpenName, OpenName, MAXFILESIZE);
MessageBox(hDlg, str, NULL,
MB_OK | MB_ICONHAND);
return (TRUE);
}
/* File is opened and there is enough room so return
* the handle to the caller.
*/
strcpy(FileName, OpenName);
EndDialog(hDlg, hFile);
return (TRUE);
}
return (TRUE);
case IDD_CANCEL:
EndDialog(hDlg, NULL);
return (TRUE);
}
break;
case WM_INITDIALOG: /* message: initialize */
UpdateListBox(hDlg);
SetDlgItemText(hDlg, IDC_EDIT, DefSpec);
SendDlgItemMessage(hDlg, /* dialog handle */
IDC_EDIT, /* where to send message */
EM_SETSEL, /* select characters */
NULL, /* additional information */
MAKELONG(0, 0x7fff)); /* entire contents */
SetFocus(GetDlgItem(hDlg, IDC_EDIT));
return (FALSE); /* Indicates the focus is set to a control */
}
return FALSE;
}
/****************************************************************************
FUNCTION: UpdateListBox(HWND);
PURPOSE: Update the list box of TraceOutput
****************************************************************************/
void UpdateListBox(hDlg)
HWND hDlg;
{
strcpy(str, DefPath);
strcat(str, DefSpec);
DlgDirList(hDlg, str, IDC_LISTBOX, IDC_PATH, 0x4010);
/* To ensure that the listing is made for a subdir. of
* current drive dir...
*/
if (!strchr (DefPath, ':'))
DlgDirList(hDlg, DefSpec, IDC_LISTBOX, IDC_PATH, 0x4010);
/* Remove the '..' character from path if it exists, since this
* will make DlgDirList move us up an additional level in the tree
* when UpdateListBox() is called again.
*/
if (strstr (DefPath, ".."))
DefPath[0] = '\0';
SetDlgItemText(hDlg, IDC_EDIT, DefSpec);
}
/****************************************************************************
FUNCTION: ChangeDefExt(PSTR, PSTR);
PURPOSE: Change the default extension
****************************************************************************/
void ChangeDefExt(Ext, Name)
PSTR Ext, Name;
{
PSTR pTptr;
pTptr = Name;
while (*pTptr && *pTptr != '.')
pTptr++;
if (*pTptr)
if (!strchr(pTptr, '*') && !strchr(pTptr, '?'))
strcpy(Ext, pTptr);
}
/****************************************************************************
FUNCTION: SeparateFile(HWND, LPSTR, LPSTR, LPSTR)
PURPOSE: Separate filename and pathname
****************************************************************************/
void SeparateFile(hDlg, lpDestPath, lpDestFileName, lpSrcFileName)
HWND hDlg;
LPSTR lpDestPath, lpDestFileName, lpSrcFileName;
{
LPSTR lpTmp;
char cTmp;
lpTmp = lpSrcFileName + (long) lstrlen(lpSrcFileName);
while (*lpTmp != ':' && *lpTmp != '\\' && lpTmp > lpSrcFileName)
lpTmp = AnsiPrev(lpSrcFileName, lpTmp);
if (*lpTmp != ':' && *lpTmp != '\\') {
lstrcpy(lpDestFileName, lpSrcFileName);
lpDestPath[0] = 0;
return;
}
lstrcpy(lpDestFileName, lpTmp + 1);
cTmp = *(lpTmp + 1);
lstrcpy(lpDestPath, lpSrcFileName);
*(lpTmp + 1) = cTmp;
lpDestPath[(lpTmp - lpSrcFileName) + 1] = 0;
}
/****************************************************************************
FUNCTION: AddExt(PSTR, PSTR);
PURPOSE: Add default extension
/***************************************************************************/
void AddExt(Name, Ext)
PSTR Name, Ext;
{
PSTR pTptr;
pTptr = Name;
while (*pTptr && *pTptr != '.')
pTptr++;
if (*pTptr != '.')
strcat(Name, Ext);
}
/****************************************************************************
FUNCTION: CheckFileName(HWND, PSTR, PSTR)
PURPOSE: Check for wildcards, add extension if needed
COMMENTS:
Make sure you have a filename and that it does not contain any
wildcards. If needed, add the default extension. This function is
called whenever your application wants to save a file.
****************************************************************************/
BOOL CheckFileName(hWnd, pDest, pSrc)
HWND hWnd;
PSTR pDest, pSrc;
{
PSTR pTmp;
if (!pSrc[0])
return (FALSE); /* Indicates no filename was specified */
pTmp = pSrc;
while (*pTmp) { /* Searches the string for wildcards */
switch (*pTmp++) {
case '*':
case '?':
MessageBox(hWnd, "Wildcards not allowed.",
NULL, MB_OK | MB_ICONEXCLAMATION);
return (FALSE);
}
}
AddExt(pSrc, DefExt); /* Adds the default extension if needed */
if (OpenFile(pSrc, (LPOFSTRUCT) &OfStruct, OF_EXIST) >= 0) {
sprintf(str, "Replace existing %s?", pSrc);
if (MessageBox(hWnd, str, "EditFile",
MB_OKCANCEL | MB_ICONHAND) == IDCANCEL)
return (FALSE);
}
strcpy(pDest, pSrc);
return (TRUE);
}
/****************************************************************************
FUNCTION: SaveFile(HWND)
PURPOSE: Save current file
COMMENTS:
This saves the current contents of the Edit buffer, and changes
bChanges to indicate that the buffer has not been changed since the
last save.
Before the edit buffer is sent, you must get its handle and lock it
to get its address. Once the file is written, you must unlock the
buffer. This allows Windows to move the buffer when not in immediate
use.
****************************************************************************/
BOOL SaveFile(hWnd)
HWND hWnd;
{
BOOL bSuccess;
int IOStatus; /* result of a file write */
if ((hFile = OpenFile(FileName, &OfStruct,
OF_PROMPT | OF_CANCEL | OF_CREATE)) < 0) {
/* If the file can't be saved */
sprintf(str, "Cannot write to %s.", FileName);
MessageBox(hWnd, str, NULL, MB_OK | MB_ICONEXCLAMATION);
return (FALSE);
}
hEditBuffer = SendMessage(hEditWnd, EM_GETHANDLE, 0, 0L);
pEditBuffer = LocalLock(hEditBuffer);
/* Set the cursor to an hourglass during the file transfer */
hSaveCursor = SetCursor(hHourGlass);
IOStatus = write(hFile, pEditBuffer, strlen(pEditBuffer));
close(hFile);
SetCursor(hSaveCursor);
if (IOStatus != strlen(pEditBuffer)) {
sprintf(str, "Error writing to %s.", FileName);
MessageBox(hWnd, str,
NULL, MB_OK | MB_ICONHAND);
bSuccess = FALSE;
}
else {
bSuccess = TRUE; /* Indicates the file was saved */
bChanges = FALSE; /* Indicates changes have been saved */
}
LocalUnlock(hEditBuffer);
return (bSuccess);
}
/****************************************************************************
FUNCTION: QuerySaveFile(HWND);
PURPOSE: Called when some action might lose current contents
COMMENTS:
This function is called whenever we are about to take an action that
would lose the current contents of the edit buffer.
****************************************************************************/
BOOL QuerySaveFile(hWnd)
HWND hWnd;
{
int Response;
FARPROC lpSaveAsDlg;
if (bChanges) {
sprintf(str, "Save current changes: %s", FileName);
Response = MessageBox(hWnd, str,
"EditFile", MB_YESNOCANCEL | MB_ICONEXCLAMATION);
if (Response == IDYES) {
check_name:
/* Make sure there is a filename to save to */
if (!FileName[0]) {
lpSaveAsDlg = MakeProcInstance(SaveAsDlg, hInst);
Response = DialogBox(hInst, "SaveAs",
hWnd, lpSaveAsDlg);
FreeProcInstance(lpSaveAsDlg);
if (Response == IDOK)
goto check_name;
else
return (FALSE);
}
SaveFile(hWnd);
}
else if (Response == IDCANCEL)
return (FALSE);
}
else
return (TRUE);
}
/****************************************************************************
FUNCTION: SetNewBuffer(HWND, HANDLE, PSTR)
PURPOSE: Set new buffer for edit window
COMMENTS:
Point the edit window to the new buffer, update the window title, and
redraw the edit window. If hNewBuffer is NULL, then create an empty
1K buffer, and return its handle.
****************************************************************************/
void SetNewBuffer(hWnd, hNewBuffer, Title)
HWND hWnd;
HANDLE hNewBuffer;
PSTR Title;
{
HANDLE hOldBuffer;
hOldBuffer = SendMessage(hEditWnd, EM_GETHANDLE, 0, 0L);
LocalFree(hOldBuffer);
if (!hNewBuffer) /* Allocates a buffer if none exists */
hNewBuffer = LocalAlloc(LMEM_MOVEABLE | LMEM_ZEROINIT, 1);
/* Updates the buffer and displays new buffer */
SendMessage(hEditWnd, EM_SETHANDLE, hNewBuffer, 0L);
SetWindowText(hWnd, Title);
SetFocus(hEditWnd);
bChanges = FALSE;
}
/****************************************************************************
FUNCTION: About(HWND, unsigned, WORD, LONG)
PURPOSE: Processes messages for "About" dialog box
MESSAGES:
WM_INITDIALOG - initialize dialog box
WM_COMMAND - Input received
****************************************************************************/
BOOL FAR PASCAL About(hDlg, message, wParam, lParam)
HWND hDlg;
unsigned message;
WORD wParam;
LONG lParam;
{
switch (message) {
case WM_INITDIALOG:
return (TRUE);
case WM_COMMAND:
if (wParam == IDOK
|| wParam == IDCANCEL) {
EndDialog(hDlg, TRUE);
return (TRUE);
}
break;
}
return (FALSE);
}